package com.supreme.threadserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonmybatis.mapper.OrderMapper;
import com.supreme.threadserver.service.ReentryAsyncService;
import com.supreme.threadserver.service.ThreadPoolService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "threadPool")
public class ThreadPoolController {

    @Resource
    private ThreadPoolService threadPoolService;

    @Resource
    private ReentryAsyncService reentryAsyncService;

    @Resource
    private OrderMapper orderMapper;

    @GetMapping(value = "simpleRun")
    public AjaxResp simpleRun() {
        return AjaxResp.instance();
    }

    @GetMapping(value = "threadPoolInfo")
    public AjaxResp threadPoolInfo() {
        return AjaxResp.instance();
    }

    @GetMapping(value = "threadPoolAdvanceTest")
    public AjaxResp threadPoolAdvanceTest(@RequestParam(value = "threadName") String threadName) {
        threadPoolService.threadPoolAdvanceTest();
        return AjaxResp.instance();
    }

    @GetMapping(value = "shutdownNow")
    public AjaxResp shutdownNow() {
        threadPoolService.shutdownNow();
        return AjaxResp.instance();
    }

    @GetMapping(value = "supremeThreadPool")
    public AjaxResp supremeThreadPool(@RequestParam(value = "threadName") String threadName) {
        threadPoolService.supremeTaskExecutor();
        return AjaxResp.instance();
    }

    @GetMapping(value = "printTaskExecutorInfo")
    public AjaxResp printTaskExecutorInfo() {
        final JSONObject jsonObject = threadPoolService.printTaskExecutorInfo();
        return AjaxResp.instance(jsonObject);
    }

    @GetMapping(value = "asyncTaskExecutor")
    public AjaxResp asyncTaskExecutor() {
        threadPoolService.asyncTaskExecutor();
        return AjaxResp.instance();
    }

    @GetMapping(value = "supremeTaskExecutor")
    public AjaxResp asyncSupremeTaskExecutor() {
        threadPoolService.asyncSupremeTaskExecutor();
        return AjaxResp.instance();
    }

    @GetMapping(value = "reentryThread")
    public AjaxResp reentryThread() {
        String threadName = Thread.currentThread().getName();
        System.out.println("ThreadPoolController reentryThreadDemo threadName = " + threadName);
        threadPoolService.reentryThreadDemo();
        reentryAsyncService.reentryThreadDemo();
        return AjaxResp.instance();
    }

    @GetMapping(value = "simpleThreadPool/{count}")
    public AjaxResp simpleThreadPool(@PathVariable(value = "count") int count) {
        orderMapper.truncateTable();
        for (int i = 0; i < count; i++) {
            threadPoolService.insertBatchAccount(i + 1);
        }
        return AjaxResp.instance();
    }
}
